<template>
  <div id="VIP_index" class="all">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>库存管理 </el-breadcrumb-item>
      <el-breadcrumb-item>入库明细</el-breadcrumb-item>
    </el-breadcrumb>
    <br>
    <div class="table-container">
      <!-- <el-descriptions title="" class="custom-descriptions"></el-descriptions> -->
      <span style="margin-right: 91%;padding: 10px;"><b>入库查询</b></span>
      <el-divider class="custom-divider"></el-divider>
      <el-form :model="selform" ref="form">
        <el-row>
          <el-col :span="4">
            <el-input v-model="selform.supplyId" style="width: 95%;padding: 10px;" placeholder="供货订单编号" />
          </el-col>
          <el-col :span="4">
            <el-input v-model="selform.goodName" style="width: 95%;padding: 10px;" placeholder="商品名称" />
          </el-col>
          <el-col :span="4">
            <el-input v-model="selform.supplierName" style="width: 95%;padding: 10px;" placeholder="供应商" />
          </el-col>
          <el-col :span="6">
            <el-date-picker v-model="selform.orderTime" type="datetimerange" :picker-options="pickerOptions"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 95%;margin:10px;"
              align="right"></el-date-picker>
          </el-col>
          <el-col :span="6" style="text-align: center;padding: 8px 10px;">
            <el-button type="primary" style="font-size: 16px;" @click="submitselMessage" icon="el-icon-search"> 搜索</el-button>
            <el-button type="success" style="font-size: 16px;" @click="openInputMessage"> 添加入库 </el-button>

            <el-dialog title="新增入库信息" :visible.sync="inputMessage">
              <el-form :model="inputform">
                <el-form-item label="商品名称" :label-width="formLabelWidth">
                  <el-input v-model="inputform.goodName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="供应商名称" :label-width="formLabelWidth">
                  <el-input v-model="inputform.supplierName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="数量" :label-width="formLabelWidth">
                  <el-input v-model="inputform.number" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="总金额" :label-width="formLabelWidth">
                  <el-input v-model="inputform.total" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="入库时间" :label-width="formLabelWidth">
                  <el-input v-model="inputform.orderTime" autocomplete="off"></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="closeInputMessage">取 消</el-button>
                <el-button type="primary" @click="submitInputMessage">确 定</el-button>
              </div>
            </el-dialog>


            <el-dialog title="更新供货订单信息" :visible.sync="updateMessage" @close="closeUpdateMessage"
              :close-on-click-modal="false">
              <el-form :model="updateform" ref="form">
                <el-form-item label="商品名称" :label-width="formLabelWidth" prop="username">
                  <el-input v-model="updateform.goodName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="供应商" :label-width="formLabelWidth">
                  <el-input v-model="updateform.supplierName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="数量" :label-width="formLabelWidth">
                  <el-input v-model="updateform.number" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="总金额" :label-width="formLabelWidth">
                  <el-input v-model="updateform.total" :readonly="true" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="入库时间" :label-width="formLabelWidth">
                  <!-- <el-input v-model="updateform.orderTime" autocomplete="off"></el-input> -->
                  <el-date-picker v-model="updateform.orderTime" type="datetime" placeholder="选择日期时间" align="right"
                    style="width: 100%;" :picker-options="pickerOptions">
                  </el-date-picker>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="closeUpdateMessage">取 消</el-button>
                <el-button type="primary" @click="submitUpdateMessage">确 定</el-button>
              </div>
            </el-dialog>

          </el-col>
        </el-row>
      </el-form>
    </div>

    <br>



    <!--表格数据-->
    <div class="table-container">
      <span style="margin-right: 91%;padding: 10px;"><b>入库信息</b></span>
      <el-divider class="custom-divider"></el-divider>
      <el-table :data="maindata" style="width: 100%" size="medium">

        <el-table-column prop="supplyId" width="120" label="供货订单编号"> </el-table-column>
        <el-table-column width="250" prop="goodName" label="商品名称"></el-table-column>
        <el-table-column width="150" prop="supplierName" label="供应商"></el-table-column>
        <el-table-column width="120" prop="number" label="数量"></el-table-column>
        <el-table-column width="120" prop="total" label="总金额"></el-table-column>
        <el-table-column width="180" prop="orderTime" label="入库时间">
          <template slot-scope="scope">
            {{ decoder(scope.row.orderTime) }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250" fixed="right">
          <template v-slot="scope">
            <el-button style="font-size: 16px" type="success" @click="openUpdateMessage(scope.row)"> 修改</el-button>
            <el-button style="font-size: 16px" type="danger" @click="deleteOpen(scope.row)"> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
        background layout="total, prev, pager, next, jumper" :total="400">
      </el-pagination>
    </div>
  </div>
</template>
  
<script>
import axios from 'axios';
export default {
  data() {
    return {
      empId: "",
      cusId: "",
      username: "",
      password: "",
      maindata: [
        // {
        //   supplyId: "1",
        //   goodName: "保温杯",
        //   supplierName: "FUGUANG",
        //   number: "50",
        //   total: "1000",
        //   orderTime: "2023-12-31T12:32:00.210Z"
        // },
        // {
        //   supplyId: "2",
        //   goodName: "特仑苏纯牛奶250*12盒整箱",
        //   supplierName: "蒙牛",
        //   number: "150",
        //   total: "7500",
        //   orderTime: "2024-01-01T09:49:51.000Z"
        // },
      ],

      inputMessage: false,
      updateMessage: false,

      selform: {
        supplyId: "",
        goodName: "",
        supplierName: "",
        orderTime: "",
      },
      inputform: {
        goodName: "",
        supplierName: "",
        number: "",
        total: "",
        orderTime: "",
      },
      updateform: {
        supplyId: "",
        goodName: "",
        supplierName: "",
        number: "",
        total: "",
        orderTime: "",
      },
      checkid() {
        if (this.empId == "" || this.empId == null)
          this.$router.push('/login');
      },
      deleteform: {
        supplyId: "",
        goodName: "",
        supplierName: "",
        number: "",
        total: "",
        orderTime: "",
      },
      formLabelWidth: '90px',
      formLabelWidthTime: '85px'
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },

    getMaindata() {
      const url = "http://localhost:8080/instore";
      axios.get(url)
        .then((response) => {
          console.log(response.data);
          this.maindata = response.data.data.rows;
        })
        .catch((error) => {
          console.error('Error in getMaindata request', error);
        });
    },

    init(){
      this.empId = localStorage.getItem('empId');
      this.cusId = localStorage.getItem('cusId');
      this.username = localStorage.getItem('username')|| "";
      this.password = localStorage.getItem('password')|| "";
    },

    selDate() {
      const url = "http://localhost:8080/instore";
      axios.get(url, {
        params: this.selform
      }).then((response) => {
        console.log(response.data);
        this.maindata = response.data.data.rows;
      })
        .catch((error) => {
          console.error('Error in selData request', error);
        });
    },

    delData() {
      let url = "http://localhost:8080/instore";
      url += this.deleteform.supplyId;
      axios.delete(url).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in delData request', error);
        });
    },

    inputData() {
      const url = "http://localhost:8080/instore";
      axios.post(url, this.inputform
      ).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in inputData request', error);
        });
    },

    updateData() {
      const url = "http://localhost:8080/instore";
      axios.put(url, this.updateform
      ).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in updateData request', error);
        });
    },


    decoder(time) {
      let arr = time.split("T");
      let str1 = arr[0];
      let str2 = arr[1].split("Z")[0].split(".")[0];
      let result = `${str1} ${str2}`;
      return result;
    },

    openInputMessage() {
      this.inputMessage = true;
    },
    closeInputMessage() {
      this.inputMessage = false;
    },
    submitInputMessage() {
      // 处理表单提交逻辑
      // 关闭对话框
      this.$message({
        type: "success",
        message: "添加成功!",
      });
      this.inputData();
      this.closeInputMessage();
      this.submitselMessage();
    },




    openUpdateMessage(row) {
      this.updateform.supplyId = row.supplyId;
      this.updateform.goodName = row.goodName;
      this.updateform.supplierName = row.supplierName;
      this.updateform.number = row.number;
      this.updateform.total = row.total;
      this.updateform.orderTime = row.orderTime;
      this.updateMessage = true;
    },
    closeUpdateMessage() {
      this.updateMessage = false;
    },
    submitUpdateMessage() {
      this.closeUpdateMessage();
      this.updateData();
      this.selDate();
      this.$message({
        type: "success",
        message: "修改成功!",
      });
    },

    submitselMessage() {
      this.selDate();
    },



    deleteOpen(row) {
      this.deleteform.supplyId = row.supplyId;
      this.deleteform.goodName = row.goodName;
      this.deleteform.supplierName = row.supplierName;
      this.deleteform.number = row.number;
      this.deleteform.total = row.total;
      this.deleteform.orderTime = row.orderTime;
      this.$confirm("此操作将删除该条会员信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.delData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.submitselMessage();
        }
        )
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    pickerOptions: {
      shortcuts: [{
        text: '今天',
        onClick(picker) {
          picker.$emit('pick', new Date());
        }
      }, {
        text: '昨天',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24);
          picker.$emit('pick', date);
        }
      }, {
        text: '一周前',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
          picker.$emit('pick', date);
        }
      }]
    },
  },
  mounted() {
    this.init();
    this.checkid();
    this.getMaindata();
  },
};
</script>
  
<style>
.router-link-active {
  text-decoration: none;
}

.table-container {
  box-shadow: 0 0 4px 1px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .12);
}

.custom-divider {
  margin: 4px 0;
  /* 调整 el-divider 的间距 */
}

.custom-descriptions {
  padding-left: 10px;
  margin-bottom: 0;
}

.all {
  line-height: 30px;
}
</style>